package com.example.tao_city.text1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tao_city.text1.SelectNullAction;

/**
 * Created by TAO_CITY on 2018/1/3.
 */

public class SelectActivity extends Activity implements View.OnClickListener{
    private ListView listView;
    private ImageView back;
    private EditText editText;
    private ImageView delete;
    private ImageView search;
    private String[] key=new String[]{"name"};
    private int[] value={R.id.select_listview_text};
    private List list=new ArrayList();
    private SimpleAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        initbutton();
        initlistview();

    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.select_back:
                finish();
                break;
            case R.id.select_delet:
                delete.setVisibility(View.INVISIBLE);
                editText.setText("");
                break;
            case R.id.select_search:
                addlist();
                break;
        }

    }
    private void initbutton(){
        back=(ImageView)findViewById(R.id.select_back);
        editText=(EditText)findViewById(R.id.select_edittext);
        delete=(ImageView)findViewById(R.id.select_delet);
        search=(ImageView)findViewById(R.id.select_search);
        listView=(ListView)findViewById(R.id.select_listview);

        back.setOnClickListener(this);
        search.setOnClickListener(this);
        delete.setOnClickListener(this);
        //当文本发生变化时调用此方法
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            //  s:textview中显示的文本； start:修改后的文本的起始范围的偏移量； before:替换的旧文本的长度；count:替换的修改文本的长度。
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(start==0 ){
                    delete.setVisibility(View.INVISIBLE);
                }else {
                    delete.setVisibility(View.VISIBLE);
                }
                if(count!=0){
                    delete.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    private void initlistview(){

        Map map=new HashMap();
        String an=SelectNullAction.ntext ;
        map.put("name",an);
        list.add(map);

        adapter=new SimpleAdapter(this,list,R.layout.select_listview,key,value);
        listView.setAdapter(adapter);
    }
    private void addlist(){
        Map map=new HashMap();
        String man= String.valueOf(editText.getText());
        map.put("name",man);
        list.add(0,map);
        adapter.notifyDataSetChanged();
    }

}
